const PizZip = require('pizzip');
const Docxtemplater = require('docxtemplater');
const fs = require('fs');
const path = require('path');


function generateDocx(input_file, data, output_file) {
    //Load the docx file as a binary
    const content = fs.readFileSync(path.resolve(__dirname, input_file), 'binary')
    const zip = new PizZip(content)
    try {
        const doc = new Docxtemplater(zip)
        doc.setData(data)
        doc.render()
        const buf = doc.getZip().generate({ type: 'nodebuffer' })
        // 如果传入output_file，则写入文件
        if (output_file) {
            fs.writeFileSync(path.resolve(__dirname, output_file), buf)
            return output_file
        }
        return buf
    } catch (error) {
        throw error
    }
}
module.exports = generateDocx



